package com.springboot.frame.backup;

import lombok.extern.slf4j.Slf4j;

import javax.sql.DataSource;
import java.io.*;
import java.sql.SQLException;

/**
 * @author liheng
 * @ClassName BackUpUtils
 * @Description
 * @date 2020-12-29 16:45
 */
@Slf4j
public class BackUpTemplate {
    private DataSource dataSource;
    private BackUpProperties backUpProperties;

    public BackUpTemplate(BackUpProperties properties, DataSource dataSource) {
        this.backUpProperties = properties;
        this.dataSource = dataSource;
    }

    public void export() {
        Schema schema = backUpProperties.getSchema();
        if (Schema.Mysqldump.equals(schema)) {
            ExportUtils.DumpData(backUpProperties);
        } else if (Schema.Jdbc.equals(schema)) {
            ExportUtils.export(backUpProperties, DBConnUtils.getConnection(backUpProperties));
        } else if (Schema.DataSource.equals(schema)) {
            try {
                ExportUtils.export(backUpProperties, dataSource.getConnection());
            } catch (SQLException throwables) {
                log.error("DataSourceBackup 失败：" + throwables.getMessage());
            }
        } else {
            throw new RuntimeException("未知或不支持的导出模式");
        }
    }


}
